import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_livepush_plugin/pusher/live_pusher_preview.dart';
import 'package:get/get.dart';
import 'package:sports_new/controller/live/live_push_logic.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/utils/widget_utils.dart';
import 'package:sports_new/view/live/items/live_expert_info.dart';
import 'package:sports_new/view/live/items/live_message_list.dart';
import 'package:sports_new/widgets/circular.dart';
import 'package:sports_new/widgets/max_bottom_sheet.dart';
import 'package:sports_new/widgets/base/qkd_button.dart';
import 'package:sports_new/widgets/base/qkd_image.dart';
import 'package:wakelock_plus/wakelock_plus.dart';

import '../../widgets/asset_item.dart';
import '../../widgets/base/qkd_text.dart';
import '../../widgets/card.dart';
import '../../widgets/share/share_sheet.dart';
import 'items/gift_effect_view.dart';
import 'items/live_share_append.dart';

class LivePushPage extends StatefulWidget {
  const LivePushPage({super.key});

  @override
  State<LivePushPage> createState() => _LivePushPageState();
}

class _LivePushPageState extends State<LivePushPage> {
  final logic = Get.put(LivePushLogic());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LivePushLogic>(
      initState: (state) => WakelockPlus.enable(),
      dispose: (state) => WakelockPlus.disable(),
      builder: (logic) {
        return AnnotatedRegion<SystemUiOverlayStyle>(
          value: const SystemUiOverlayStyle(
            statusBarBrightness: Brightness.light,
            statusBarIconBrightness: Brightness.light
          ),
          child: PopScope(
            canPop: false,
            child: Scaffold(
              body: Stack(
                alignment: Alignment.topCenter,
                children: [
                  AlivcPusherPreview(
                    viewType: AlivcPusherPreviewType.base,
                    onCreated: (id) async{},
                    x: 0, y: 0,
                    width: Get.width, height: Get.height
                  ),
                  if(!logic.onPush)...[
                    if(!logic.clickStart)
                    Positioned(
                      bottom: 20,
                      left: 0,right: 0,
                      child: Align(
                        alignment: Alignment.bottomCenter,
                        child: startButton()
                      )
                    )
                    else
                    Positioned.fill(
                      child: Center(
                        child: QkdText("${logic.count}",
                          fontSize: 120,
                          fontWeight: FontWeight.w500,
                          fontFamily: "Din",
                          color: Colours.white
                        )
                      )
                    )
                  ]else...[
                    LiveMessageList(
                      scene: MessageScene.push,
                      liveDetail: logic.liveDetail
                    ).paddingOnly(bottom: 20)
                  ],
                  const GiftEffect(),
                  Positioned(
                      top: 0,left: 0,right: 0,
                      child: topBar()
                  ),
                  Positioned(
                    bottom: 20,right: 0,
                    child: Padding(
                      padding: const EdgeInsets.only(right: 8),
                      child: Image.asset(
                        Utils.getImgPath('live_push_more.png'),width: 42
                      ),
                    ).tap(() => Get.bottomSheet(
                      more(),
                      isScrollControlled: true,
                      barrierColor: Colours.transparent
                    ))
                  ),
                ],
              ),
            ),
          ),
        );
      }
    );
  }

  Widget topBar(){
    return SafeArea(
      child: Container(
        padding: const EdgeInsets.symmetric(vertical: 4),
        child: Row(
          children: [
            if(logic.onPush)
            LiveExpertInfo.push(
              liveDetail: logic.liveDetail,
            ),
            const Spacer(),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 8,vertical: 10),
              child: Image.asset(
                Utils.getImgPath("close.png"),
                width: 24,color: Colours.white
              ),
            ).tap(logic.exit),
            Container(width: 16),
            // Block(
            //   onTap: () async{
            //     Get.bottomSheet(
            //         ShareSheet(append: LiveShareAppend(liveId: logic.info.id??0)),
            //         settings: RouteSettings(
            //             arguments: ShareEntity(
            //               title: "【直播】这个专家超厉害，快来围观TA的直播",
            //               content: "免费比赛分析，尽在球客岛App",
            //             )
            //         )
            //     );
            //   },
            //   cardStyle: const CardStyle(
            //       color: Colours.transparent,
            //       padding: EdgeInsets.only(right: 16)
            //   ),
            //   child: Image.asset(
            //     Utils.getImgPath("live_share.png"),
            //     color: Colours.white,
            //     width: 24,
            //     height: 24,
            //   ),
            // )
          ],
        ),
      ),
    );
  }

  Widget userInfo(){
    return Circular(
      width: 157,
      height: 36,
      margin: const EdgeInsets.only(left: 8),
      padding: const EdgeInsets.all(2),
      borderRadius: const BorderRadius.horizontal(left: Radius.circular(36)),
      gradient: LinearGradient(
        colors: [
          Colours.black.withOpacity(0.5),
          Colours.black.withOpacity(0)
        ]
      ),
      child: Row(
        children: [
          QkdImage(
            imageUrl: logic.liveDetail?.expertLogo,
            width: 32,height: 32,
            shape: BoxShape.circle,
            fit: BoxFit.cover,
            shimmer: false,
          ),
          Container(width: 8),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                QkdText(logic.liveDetail?.expertName??"",
                  fontSize: 12,color: Colours.white,
                  fontWeight: FontWeight.w500,
                  maxLines: 1,
                ),
                Opacity(
                  opacity: 0.5,
                  child: Row(
                    children: [
                      Image.asset(
                        Utils.getImgPath("live_hot.png"),
                        width: 12,height: 12,
                        color: Colours.white,
                      ),
                      QkdText("${logic.liveDetail?.hot??0}",
                        fontSize: 12,color: Colours.white,
                      ),
                    ],
                  ),
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  Widget startButton(){
    return GestureDetector(
      onTap: logic.start,
      child: const Circular(
        radius: 23.5,
        width: 232,height: 44,
        alignment: Alignment.center,
        gradient: LinearGradient(
          colors: [Color(0xFFF54D4F),Color(0xFFF53C6A)],
        ),
        child: QkdText("开始直播",
          fontSize: 16,color: Colours.white,
          fontWeight: FontWeight.w500
        ),
      ),
    );
  }
  
  Widget more(){
    return MaxBottomSheet(
      onClosing: (){},
        clipBehavior: Clip.antiAlias,
        shape: const RoundedRectangleBorder(
          borderRadius: BorderRadius.vertical(top: Radius.circular(12))
        ),
      builder: (context){
        return SafeArea(
          child: Container(
            color: const Color(0xFF111111),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                QkdText("更多工具",
                  color: Colours.white.withOpacity(0.5),fontSize: 12
                ).paddingOnly(top: 12,left: 16),
                Row(
                  children: [
                    Container(
                      width: 80,
                      alignment: Alignment.center,
                      child: AssetItem(
                        asset: "push_switch_camera.png",
                        name: "镜头反转",
                        assetSize: 32,
                        textStyle: TextStyle(fontSize: 12,color: Colours.white.withOpacity(0.5)),
                        onTap: logic.setCamera,
                      ),
                    ),
                    Container(
                      width: 80,
                      alignment: Alignment.center,
                      child: AssetItem(
                        asset: "push_mirror.png",
                        name: "镜像",
                        assetSize: 32,
                        textStyle: TextStyle(fontSize: 12,color: Colours.white.withOpacity(0.5)),
                        onTap: logic.setMirror,
                      ),
                    )
                  ].insertEvery(Container(width: 8)),
                ).paddingOnly(top: 16,bottom: 16,left: 18,right: 18)
              ],
            ),
          ),
        );
      }
    );
  }

}
